package com.cskaoyan.shopping.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.dto.ProductDto;
import com.cskaoyan.shopping.constants.GlobalConstants;
import com.cskaoyan.shopping.constants.ShoppingRetCode;
import com.cskaoyan.shopping.dto.CartProductDto;
import com.cskaoyan.shopping.form.CartRequest;
import com.cskaoyan.shopping.form.CheckAllItemRequest;
import com.cskaoyan.shopping.service.ICartService;
import com.cskaoyan.shopping.service.IProductCateService;
import com.cskaoyan.shopping.service.IProductService;
import com.cskaoyan.shopping.service.impl.cache.CacheManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;


/**
 * @Author lixinhang
 * @Time 2022/9/20 21:00
 */
@Slf4j
@RestController
@RequestMapping("/shopping")
public class CartsController {
    @Autowired
    CacheManager cacheManager;

    @Autowired
    ICartService iCartService;

    /**
     * 获得该用户的购物车列表
     * @return
     */
    @GetMapping("/carts")
    public ResponseData listShoppingCarts(HttpServletRequest request){
        //代码执行到这里，表示cookie中的access_token不为空，于是请求头user_info也不为空
        String userInfo = request.getHeader("user_info");
        JSONObject jsonObject = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(jsonObject.get("uid").toString());

        //购物车是存储在redis里的，需要使用redisson去取出来
        List<CartProductDto> cartProductDtos = iCartService.getCartListById(uid);

        return new ResponseUtil().setData(cartProductDtos);

    }

    /**
     * 添加商品至购物车
     */
    @PostMapping("carts")
    public ResponseData addShoppingCarts(@Valid @RequestBody CartRequest request){
        request.setChecked("true");
        //将请求交给iCartService处理，将请求中的商品添加至用户的购物车
        iCartService.addToCart(request);
        return new ResponseUtil().setData(ShoppingRetCode.SUCCESS.getMessage(), GlobalConstants.SUCCESS_MESSAGE);
    }


    /**
     * 更新购物车中的商品
     */
    @RequestMapping(path="/carts",method = {RequestMethod.PUT} )
    public ResponseData updateShoppingCarts(@Valid @RequestBody CartRequest cartRequest){
        iCartService.updateCartNum(cartRequest);
        return  new ResponseUtil().setData(ShoppingRetCode.SUCCESS.getMessage(),GlobalConstants.SUCCESS_MESSAGE);
    }

    /**
     * 删除购物车中的某商品
     * @return
     */
    @RequestMapping(path="/carts/{uid}/{pid}",method = RequestMethod.DELETE)
    public ResponseData deleteShoppingCartsById(@PathVariable("uid") Long uid,
                                                @PathVariable("pid") Long pid){
        iCartService.deleteCartItem(uid,pid);
        return new ResponseUtil().setData(ShoppingRetCode.SUCCESS.getMessage(),GlobalConstants.SUCCESS_MESSAGE);
    }

    /***
     * @Description //单个商品从购物车里删除
     *
     * @Param [uid, productId, request]
     * @return com.cskaoyan.mall.commons.result.ResponseData
     * @Date 2022/9/22 11:32
     * @Author ljh
     **/
    @RequestMapping(value = "items/{uid}/{productId}",method = RequestMethod.DELETE)
    public ResponseData delete(@PathVariable("uid") @NotNull Long uid,
                               @PathVariable("productId") @NotNull Long productId,
                                HttpServletRequest request){
        if(request.getHeader("user_info") == null){
            throw new BizException("用户未登录");
        }
        iCartService.deleteCartItem(uid,productId);
        return new ResponseUtil<>().setData(null);
    }

    @RequestMapping(value = "items/{uid}",method = RequestMethod.DELETE)
    public ResponseData delete(@PathVariable("uid") @NotNull Long uid,
                               HttpServletRequest request){
        if(request.getHeader("user_info") == null){
            throw new BizException("用户未登录");
        }
        iCartService.deleteCheckedItem(uid);
        return new ResponseUtil<>().setData(null);
    }


    @RequestMapping(value = "items",method = RequestMethod.PUT)
    public ResponseData putAll(@RequestBody CheckAllItemRequest cartRequest,
                               HttpServletRequest request){
        if(request.getHeader("user_info") == null){
            throw new BizException("用户未登录");
        }
        iCartService.checkAllCartItem(cartRequest);

        return new ResponseUtil<>().setData(null);
    }

}
